package com.qf.updatealt;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import com.qf.utils.SmallTool;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * @Author: gcr
 * @Date: 2022-7-11 15:40
 */
public class UpdateTbl {
    @SneakyThrows
    public static void main(String[] args) {

        String from = "altmetric_2022_05_28";
        String to = "altmetric_copy_update";
        // 更新原则 如果存在且存在有效数据则更新 不存在则不更新 且也不进行删除
        Db useLocal = Db.use();
        Db useServer = Db.use(DSFactory.get("server_core"));

        String sql = "select max(id) as count from " + from;
        Integer max = Integer.parseInt(useLocal.queryString(sql));
        StackTraceElement[] stackTrace = new Throwable().getStackTrace();
        String fileName = stackTrace[0].getFileName();
        SmallTool.printMess("开始了+++" + fileName.replace(".java", ""));

        int breakSize = 1000;
        int page = max / breakSize + 1;
        SmallTool.printMess("最大值+++" + max);
        SmallTool.printMess("页码+++" + page);
        for (int i = 1; i <= page; i++) {
            int start = breakSize * (i - 1) + 1;
            int end = breakSize * i;
            SmallTool.printMess("开始页码+" + i + "+++" + start + "++++end+++" + end);
            String querySql = "select *  from " + from + " where id between " + start + " and " + end;
            List<Entity> queryMain = useLocal.query(querySql);
            for (Entity entity : queryMain) {
                String json = entity.getStr("json");
                Integer score = entity.getInt("score");
                Integer status = entity.getInt("status");
                int pmid = entity.getInt("pmid");
                int fromid = entity.getInt("id");
                boolean b = checkIsAVA(json);
                boolean b1 = checkIsAVAGeZero(score);
                if (b && b1 && status == null) {
                    try {
                        Entity where = new Entity();
                        where.setTableName(to);
                        where.set("pmid", pmid);
                        where.set("score", score);
                        where.set("json", json);
                        int pmid1 = useServer.insertOrUpdate(where, "pmid");
                        Entity wherelocal = new Entity();
                        wherelocal.setTableName(from);
                        wherelocal.set("id", fromid);
                        entity.set("status", 0);
                        useLocal.update(entity, wherelocal);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                } else {
                    // 删除
                    useLocal.del(entity);


                }
            }
        }
    }

    // 判断字符存在且不包含特殊字符
    public static boolean checkIsAVA(String str) {
        boolean isT = false;
        if (StringUtils.isNotEmpty(str) && !str.contains("Not Found")) {
            isT = true;
        }
        return isT;
    }

    // 判断有效且大于0
    public static boolean checkIsAVAGeZero(Integer sc) {
        boolean isT = false;
        if (sc != null && sc.intValue() > 0) {
            isT = true;
        }

        return isT;
    }
}
